package com.ruoyi.qbms.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.BaseSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qbms.domain.vo.QbmsChapterDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.qbms.mapper.QbmsChapterMapper;
import com.ruoyi.qbms.domain.QbmsChapter;
import com.ruoyi.qbms.service.IQbmsChapterService;

/**
 * 题库章节Service业务层处理
 * 
 * @author zouhuu
 * @date 2022-10-16
 */
@Service
public class QbmsChapterServiceImpl implements IQbmsChapterService 
{
    @Autowired
    private QbmsChapterMapper qbmsChapterMapper;

    /**
     * 客户端查询章节数据
     * @param qbmsChapter
     * @return
     */
    @Override
    public List<QbmsChapterDataVo> clientSelectChapterData(QbmsChapter qbmsChapter) {
        return qbmsChapterMapper.clientSelectChapterData(qbmsChapter);
    }

    /**
     * 客户端查询收藏章节数据
     * @param qbmsChapter
     * @return
     */
    @Override
    public List<QbmsChapterDataVo> clientSelectFavoriteChapterData(QbmsChapter qbmsChapter) {
        return qbmsChapterMapper.clientSelectFavoriteChapterData(qbmsChapter);
    }
    /**
     * 查询笔记章节数据
     * @param qbmsChapter
     * @return
     */
    @Override
    public List<QbmsChapterDataVo> clientSelectNoteChapterData(QbmsChapter qbmsChapter) {
        return qbmsChapterMapper.clientSelectNoteChapterData(qbmsChapter);
    }

    /**
     * 查询错答章节数据
     * @param qbmsChapter
     * @return
     */
    @Override
    public List<QbmsChapterDataVo> clientSelectMistakeAnswerChapterData(QbmsChapter qbmsChapter) {
        return qbmsChapterMapper.clientSelectMistakeAnswerChapterData(qbmsChapter);
    }

    /**
     * 跟进章节ID列表查询章节数据
     * @param ids
     * @return
     */
    @Override
    public List<QbmsChapter> selectQbmsChapterByIds(Collection<Long> ids) {
        return qbmsChapterMapper.selectQbmsChapterByIds(ids);
    }

    /**
     * 查询题库章节
     * 
     * @param id 题库章节主键
     * @return 题库章节
     */
    @Override
    public QbmsChapter selectQbmsChapterById(Long id)
    {
        return qbmsChapterMapper.selectQbmsChapterById(id);
    }

    /**
     * 查询题库章节列表
     * 
     * @param qbmsChapter 题库章节
     * @return 题库章节
     */
    @Override
    public List<QbmsChapter> selectQbmsChapterList(QbmsChapter qbmsChapter)
    {
        return qbmsChapterMapper.selectQbmsChapterList(qbmsChapter);
    }

    /**
     * 查询章节选项
     * @param qbmsChapter
     * @return
     */
    @Override
    public List<QbmsChapter> selectQbmsChapterOptions(QbmsChapter qbmsChapter) {
        return qbmsChapterMapper.selectQbmsChapterOptions(qbmsChapter);
    }

    /**
     * 查询章节树选项
     * @param qbmsChapter
     * @return
     */
    @Override
    public List<BaseSelect> selectQbmsChapterTreeselect(QbmsChapter qbmsChapter) {
        // 查询章节列表
        List<QbmsChapter> chapterList = qbmsChapterMapper.selectQbmsChapterOptions(qbmsChapter);
        // 按 分类 和 科目 进行分组
        Map<String, Map<String, List<QbmsChapter>>> categoryMap = chapterList.stream()
                .collect(Collectors.groupingBy(QbmsChapter::getCategoryName,
                        Collectors.groupingBy(QbmsChapter::getSubjectName)));
        // 形成树结构
        ArrayList<BaseSelect> baseSelects = new ArrayList<>();
        // 遍历分类
        categoryMap.forEach((onekey, onevalue) -> {
            BaseSelect oneSelect = new BaseSelect();
            ArrayList<BaseSelect> oneChild = new ArrayList<>();
            // 遍历科目
            onevalue.forEach((twokey, twovalue) -> {
                BaseSelect twoSelect = new BaseSelect();
                List<BaseSelect> threeSelects = twovalue.stream().map(item -> new BaseSelect(item.getId(), item.getName())).collect(Collectors.toList());
                twoSelect.setId(0L);
                twoSelect.setLabel(twokey);
                twoSelect.setChildren(threeSelects);
                oneChild.add(twoSelect);

            });
            oneSelect.setId(0L);
            oneSelect.setLabel(onekey);
            oneSelect.setChildren(oneChild);
            baseSelects.add(oneSelect);
        });
        return baseSelects;
    }

    /**
     * 新增题库章节
     * 
     * @param qbmsChapter 题库章节
     * @return 结果
     */
    @Override
    public int insertQbmsChapter(QbmsChapter qbmsChapter)
    {
        qbmsChapter.setCreateTime(DateUtils.getNowDate());
        qbmsChapter.setCreateBy(SecurityUtils.getUsername());
        qbmsChapter.setUserId(SecurityUtils.getUserId());
        qbmsChapter.setDeptId(SecurityUtils.getDeptId());
        return qbmsChapterMapper.insertQbmsChapter(qbmsChapter);
    }

    /**
     * 修改题库章节
     * 
     * @param qbmsChapter 题库章节
     * @return 结果
     */
    @Override
    public int updateQbmsChapter(QbmsChapter qbmsChapter)
    {
        qbmsChapter.setUpdateTime(DateUtils.getNowDate());
        qbmsChapter.setUpdateBy(SecurityUtils.getUsername());
        return qbmsChapterMapper.updateQbmsChapter(qbmsChapter);
    }

    /**
     * 批量删除题库章节
     * 
     * @param ids 需要删除的题库章节主键
     * @return 结果
     */
    @Override
    public int deleteQbmsChapterByIds(Long[] ids)
    {
        return qbmsChapterMapper.deleteQbmsChapterByIds(ids);
    }

    /**
     * 删除题库章节信息
     * 
     * @param id 题库章节主键
     * @return 结果
     */
    @Override
    public int deleteQbmsChapterById(Long id)
    {
        return qbmsChapterMapper.deleteQbmsChapterById(id);
    }
}
